﻿using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ZigZagConversion
{
    public class Solution
    {
        public string Convert(string s, int numRows)
        {
            if (numRows == 1)
            {
                return s;
            }

            // 初始化存储数组
            var zigzag = new List<char>[numRows];
            for (int i = 0; i < numRows; i++)
            {
                zigzag[i] = new List<char>();
            }

            // 转换
            bool isIncrease = true;
            for (int i = 0, j = 0; i < s.Length; i++)
            {
                zigzag[j].Add(s[i]);

                if (j == 0)
                {
                    isIncrease = true;
                }
                if (j >= numRows)
                {
                    isIncrease = false;
                }

                j = isIncrease ? j + 1 : j - 1;
            }

            // List数组转字符串
            StringBuilder zigzagBuilder = new StringBuilder();
            foreach (var t in zigzag)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var t1 in t)
                {
                    sb.Append(t1);
                }

                zigzagBuilder.Append(sb);
            }
            
            return zigzagBuilder.ToString();

        }
    }
}